// weakMap防止内存泄露，弱映射的键不会被垃圾回收器考虑
const map = new WeakMap()
const observer = new ResizeObserver((entries) => {
    for (const entry of entries) {
        const handler = map.get(entry.target)
        if (handler) {
            handler({
                width: entry.borderBoxSize[0].inlineSize, // 从上到下布局为高
                height: entry.borderBoxSize[0].blockSize, // 从上到下布局为宽
            })
        }
    }
})

export default {
    mounted(el, binding) {
        map.set(el, binding.value)
        observer.observe(el)
    },
    unmounted(el) {
        observer.unobserve(el)
    }
}